package com.jmy.controller;

import com.alibaba.druid.util.StringUtils;
import com.jmy.pageresult.PageResult;
import com.jmy.pojo.Admin;
import com.jmy.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: VE
 * @DATE: 2022/9/27 14:23
 * 描述:
 */
@RestController
public class AdminController {

    private Logger logger = LoggerFactory.getLogger(AdminController.class);
    @Autowired
    private AdminService adminService;

    @GetMapping("adminLogin")
    public PageResult login(Admin admin, HttpSession session){
        logger.info("用户登录");
        if(admin==null|| StringUtils.isEmpty(admin.getAName())||StringUtils.isEmpty(admin.getAPass())){
            return PageResult.none("用户名或密码不能为空");
        }
        Admin admin1 = adminService.getAdminByName(admin.getAName());
        System.out.println(admin.getAPass()+"--"+admin.getAName());
        if(admin1==null||!admin.getAPass().equals(admin1.getAPass())){
            return PageResult.none("用户名或密码错误");
        }
        session.setAttribute("admin",admin1);
        return PageResult.success("登录成功",admin1);
    }

    @GetMapping("admin")
    public PageResult getAdminList(){
        logger.info("获取用户列表");
        List<Admin> adminList = adminService.getAdminList();
        return PageResult.success("获取成功!",adminList);
    }
    @PutMapping("admin")
    public PageResult updateAdminRoles( @RequestParam("aid") Integer aid, @RequestParam("role") String role){
        logger.info("修改用户角色列表");
        String []roles = role.split(",");
        adminService.updateAdminRoles(aid,roles);
        return PageResult.none("修改成功");
    }
}
